<?php
header("Content-type: application/x-shockwave-flash");

// retrieve ICS URL
function getFileUrl() {
	return $_GET['src'];
}


// load ICS file
function loadFile($url) {
	$ics = '';
	$icsFile = fopen($url, 'r');
	while(!feof($icsFile)) {
		$ics = $ics . fread($icsFile, 1024);
	}
	fclose($icsFile);
	return $ics;
}


function eventSort($a, $b) {
	if($a['DTSTART'] > $b['DTSTART']) {
		return -1;
	}
	else {
		return 1;
	}
}


// parse into an array of events
function buildEvents($eventsSrc) {
	$BEGIN = 'BEGIN:';
	$VEVENT = 'VEVENT';
	$veventLen = count($vevent);
	// split on something with regex
	$beginArray = explode($BEGIN, $eventsSrc);
	$eventsArray = array();

	for($i = 0; $i < count($beginArray); $i++) {
		$pos = strpos($beginArray[$i], $VEVENT);
		if($pos === 0) {
			$eventsArray[] = buildSingleEvent(str_replace("\n ", "", $beginArray[$i]));
		}
	}

	uasort($eventsArray, "eventSort");
	return $eventsArray;
}


function getValidProperties() {
	$validProperties = array();
	$validProperties['LOCATION'] = true;
	$validProperties['DTSTAMP'] = true;
	$validProperties['DTSTART'] = true;
	$validProperties['DTEND'] = true;
	$validProperties['UID'] = true;
	$validProperties['SUMMARY'] = true;
	$validProperties['DESCRIPTION'] = true;
	$validProperties['STATUS'] = true;
	$validProperties['CLASS'] = true;
	return $validProperties;
}


function buildSingleEvent($eventsrc) {
	// parse string into associative array with for i in...
	$NEW_LINE = "\n";
	$COLON = ':';
	$SEMICOLON = ';';
	$validProperties = getValidProperties();
	$event = array();
	$parts = explode($NEW_LINE, $eventsrc);
	$partsLen = count($parts);
	$lastValidKey = 'NULL';
	for($i = 0; $i < $partsLen; $i++) {
		$pos = strpos($parts[$i], $COLON);
		if($pos !== false) {
			$nameValue = explode($COLON, $parts[$i]);
			$value = trim($nameValue[1]);

			$pos = strpos($nameValue[0], $SEMICOLON);
			if($pos !== false) {
				$nameParts = explode($SEMICOLON, $nameValue[0]);
				$name = trim($nameParts[0]);
			}
			else {
				$name = trim($nameValue[0]);
			}

			$event[$name] = $value;

//			if($validProperties[$name]) {
//				$lastValidKey = $name;
//				$event[$lastValidKey] = $value;
//			}
//			else {
				// if colon exists, but isn't part of a property separator then append string to the previous valid property
//				$event[$lastValidKey] = $event[$lastValidKey] . $NEW_LINE . trim($parts[$i]);
//			}
		}
		else {
			// if no colon exists then append string to the previous valid property
			if($lastValidKey != 'NULL') {
				$event[$lastValidKey] = $event[$lastValidKey] . $NEW_LINE . trim($parts[$i]);
			}
		}
	}
	return $event;
}


function convertTime($hour, $minute) {
	if($hour > 12) {
		$time = ($hour - 12) . ':' . $minute . 'PM';
	}
	else if($hour == 12) {
		$time = '12:' . $minute . 'PM';
	}
	else if($hour < 1) {
		if($minute < 1) {
			$time = '12:00AM';
		}
		else {
			$time = '12:' . $minute . 'AM';
		}
	}
	else {
		$time = trimZeros($hour) . ':' . $minute . 'AM';
	}
	return $time;
}


function trimZeros($num) {
	if($num[0] == 0) {
//	if($num[0] == 0 && count($num) > 1) {
		return $num[1];
	}
	else {
		return $num;
	}
}


function buildSwf($eventsReversed) {
  	$events = array_reverse($eventsReversed);
	ming_useswfversion(4);
	$movie = new SWFMovie();
	$movie -> setDimension(1,1);
	$movie -> setBackground(255, 255, 255);
	$UID = 'UID';
	$DTSTART = 'DTSTART';
	$DTEND = 'DTEND';
	$SUMMARY = 'SUMMARY';
	$DESCRIPTION = 'DESCRIPTION';
	$LOCATION = 'LOCATION';
	$T = 'T';
	$count = 0;
	$startYear = -1;
	$startMonth = -1;
	$startDay = -1;
	$startHour = -1;
	$startMinute = -1;
	$startSecond = -1;
	$endYear = -1;
	$endMonth = -1;
	$endDay = -1;
	$endHour = -1;
	$endMinute = -1;
	$endSecond = -1;
	$difYear = 0;
	$difMonth = 0;
	$difDay = 0;
	$allDayStartHour = 0;
	$allDayStartMinute = 0;
	$allDayStartSecond = 0;
	$allDayEndHour = 23;
	$allDayEndMinute = 59;
	$allDayEndSecond = 59;
	$varName = "";
	$varValue = "";
	$asVariables = array();
	$movieClips = array();
	$movieClipevents = array();
	$eventsLen = count($events);
	$onLoadAction = 'tellTarget("../../"){callFrame("onDataLoaded");};';
	$movie -> add(new SWFAction($onLoadAction));
	for($i = 0; $i < $eventsLen; $i++) {
		// -1. determine year, month and day of event
		$startStamp = $events[$i][$DTSTART];

		$endStamp = $events[$i][$DTEND];
		if(is_null($startStamp)) {
			$startStamp = $events[$i][$UID];
			if(is_null($endStamp)) {
				$endStamp = $startStamp;
			}
		}

		if(substr($startStamp, -7, 1) == $T) {
			$startYear = substr($startStamp, -15, 4);
			$startMonth = substr($startStamp, -11, 2);
			$startDay = substr($startStamp, -9, 2);
			$startHour = substr($startStamp, -6, 2);
			$startMinute = substr($startStamp, -4, 2);
			$startSecond = substr($startStamp, -2, 2);
			$endYear = substr($endStamp, -15, 4);
			$endMonth = substr($endStamp, -11, 2);
			$endDay = substr($endStamp, -9, 2);
			$endHour = substr($endStamp, -6, 2);
			$endMinute = substr($endStamp, -4, 2);
			$endSecond = substr($endStamp, -2, 2);
		}
		else {
			$startYear = substr($startStamp, -8, 4);
			$startMonth = substr($startStamp, -4, 2);
			$startDay = substr($startStamp, -2, 2);
			$startHour = $allDaystartHour;
			$startMinute = $allDaystartMinute;
			$startSecond = $allDaystartSecond;
			$endYear = substr($endStamp, -8, 4);
			$endMonth = substr($endStamp, -4, 2);
			$endDay = substr($endStamp, -2, 2);
			$endHour = $allDayEndHour;
			$endMinute = $allDayEndMinute;
			$endSecond = $allDayEndSecond;
		}

		$startDate = $startMonth . '/' . $startDay . '/' . $startHour;
		$endDate = $endMonth . '/' . $endDay . '/' . $endHour;
		$startTime = convertTime($startHour, $startMinute);
		$endTime = convertTime($endHour, $endMinute);
		$startMonth = trimZeros($startMonth);
		$endMonth = trimZeros($endMonth);
		$startDay = trimZeros($startDay);
		$endDay = trimZeros($endDay);

		// 1 1/2. is end date > start date (sans hours)
		$start = mktime(0, 0, 0, $startMonth, $startDay, $startYear);
		$end = mktime(0, 0, 0, $endMonth, $endDay, $endYear);
		$diff = $end - $start;

		// 0. determine if event spans many days
		$secInDay = 86400;
		$daysSpanned = 1 + $diff/$secInDay;
		if($daysSpanned == 2 && $startTime == "12:00AM" && $endTime == "11:59PM") {
			$daysSpanned = 1;
		}
		$today = $start;
		$currentYear = $startYear;
		$currentMonth = $startMonth;
		$currentDay = $startDay;

		for($j = 0; $j < $daysSpanned; $j++) {
			// 1. write variables for each day with events
			$varName = 'y' . trimZeros($currentYear) . '_m' . trimZeros($currentMonth) . '_d' . trimZeros($currentDay);

			if($j == 0) {
				$currentStartHour = $startHour;
				$currentStartMinute = $startMinute;
				$currentStartSecond = $startSecond;
			}
			else {
				$currentStartHour = $allDayStartHour;
				$currentStartMinute = $allDayStartMinute;
				$currentStartSecond = $allDayStartSecond;
			}

			if($j == ($daysSpanned - 1)) {
				$currentEndHour = $endHour;
				$currentEndMinute = $endMinute;
				$currentEndSecond = $endSecond;
			}
			else {
				$currentEndHour = $allDayEndHour;
				$currentEndMinute = $allDayEndMinute;
				$currentEndSecond = $allDayEndSecond;
			}

			if(is_null($asVariables[$varName])) {
				$varValue = 'd' . $count++;
				$asVariables[$varName] = $varValue;
				$movie -> add(new SWFAction($varName . '="' . $varValue . '";'));

				// 2. create a MovieClip for each day with events
				$libClip = new SWFSprite();

				// 3. attach event data to MovieClips
				$libClip -> add(new SWFAction('e1_uid="' . $events[$i][$UID] . '";'));
				$libClip -> add(new SWFAction('e1_summary="' . rawurlencode($events[$i][$SUMMARY]) . '";'));
				$libClip -> add(new SWFAction('e1_location="' . rawurlencode($events[$i][$LOCATION]) . '";'));
				$libClip -> add(new SWFAction('e1_description="' . rawurlencode($events[$i][$DESCRIPTION]) . '";'));
				$libClip -> add(new SWFAction('e1_startDate="' . rawurlencode($startDate) . '";'));
				$libClip -> add(new SWFAction('e1_startTime="' . rawurlencode($startTime) . '";'));
				$libClip -> add(new SWFAction('e1_endTime="' . rawurlencode($endTime) . '";'));
				$libClip -> add(new SWFAction('e1_startHour="' . $currentStartHour . '";'));
				$libClip -> add(new SWFAction('e1_startMinute="' . $currentStartMinute . '";'));
				$libClip -> add(new SWFAction('e1_startSecond="' . $currentStartSecond . '";'));
				$libClip -> add(new SWFAction('e1_endHour="' . $currentEndHour . '";'));
				$libClip -> add(new SWFAction('e1_endMinute="' . $currentEndMinute . '";'));
				$libClip -> add(new SWFAction('e1_endSecond="' . $currentEndSecond . '";'));

  				$libClip -> nextFrame();

				$clip = $movie -> add($libClip);
				$clip -> setName($varValue);

				$movieClips[$varName] = $libClip;
				$movieClipEvents[$varName] = 1;
			}
			else {
				$libClip = $movieClips[$varName];
				$totalMcEvents = $movieClipEvents[$varName] + 1;
				$movieClipEvents[$varName] = $totalMcEvents;
				$libClip -> add(new SWFAction('e' . $totalMcEvents . '_uid="' . $events[$i][$UID] . '";'));
				$libClip -> add(new SWFAction('e' . $totalMcEvents . '_summary="' . rawurlencode($events[$i][$SUMMARY]) . '";'));
				$libClip -> add(new SWFAction('e' . $totalMcEvents . '_location="' . rawurlencode($events[$i][$LOCATION]) . '";'));
				$libClip -> add(new SWFAction('e' . $totalMcEvents . '_description="' . rawurlencode($events[$i][$DESCRIPTION]) . '";'));
				$libClip -> add(new SWFAction('e' . $totalMcEvents . '_startDate="' . rawurlencode($startDate) . '";'));
				$libClip -> add(new SWFAction('e' . $totalMcEvents . '_startTime="' . rawurlencode($startTime) . '";'));
				$libClip -> add(new SWFAction('e' . $totalMcEvents . '_endTime="' . rawurlencode($endTime) . '";'));
				$libClip -> add(new SWFAction('e' . $totalMcEvents . '_startHour="' . $currentStartHour . '";'));
				$libClip -> add(new SWFAction('e' . $totalMcEvents . '_startMinute="' . $currentStartMinute . '";'));
				$libClip -> add(new SWFAction('e' . $totalMcEvents . '_startSecond="' . $currentStartSecond . '";'));
				$libClip -> add(new SWFAction('e' . $totalMcEvents . '_endHour="' . $currentEndHour . '";'));
				$libClip -> add(new SWFAction('e' . $totalMcEvents . '_endMinute="' . $currentEndMinute . '";'));
				$libClip -> add(new SWFAction('e' . $totalMcEvents . '_endSecond="' . $currentEndSecond . '";'));

  				$libClip -> nextFrame();
			}

			$tomorrow = mktime(0, 0, 0, $currentMonth, $currentDay + 1, $currentYear);
			$currentYear = substr(date("Ymd", $tomorrow), -8, 4);
			$currentMonth = substr(date("Ymd", $tomorrow), -4, 2);
			$currentDay = substr(date("Ymd", $tomorrow), -2, 2);
		}

	}

 	foreach($movieClips as $key => $mc) {
 		$clipActions = 'numOfEvents=' . $movieClipEvents[$key] . ';';
 		$mc -> add(new SWFAction($clipActions));
		$mc -> nextFrame();
 	}

	return $movie;
}

$url = getFileUrl();
$ics = loadFile($url);
$events = buildEvents($ics);
$movie = buildSwf($events);

$movie -> output();
?>
